package juc;

class MyRunnable implements Runnable{
     boolean flag = false;
    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("线程内把flag变为true了");
    }

    public boolean isFlag() {
        synchronized (this) {
            return flag;
        }
    }

    public void setFlag(boolean flag) {
        synchronized (this) {
            this.flag = flag;
        }
    }
}

public class Question {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread myThread = new Thread(myRunnable);
        System.out.println("myRunnable.flag="+myRunnable.flag);
        myThread.start();
        while (true){
            if (myRunnable.isFlag()){
                System.out.println("------------------");
                break;
            }
        }
    }

}
